Custom Exception তৈরি এবং হ্যান্ডল করা

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) - Error Handling এবং Exception Management
131

স্প্রিং বুটে কাস্টম এক্সেপশন তৈরি এবং হ্যান্ডল করার মাধ্যমে API কলের সময় এরর ম্যানেজমেন্ট আরও কার্যকর করা যায়। নিচে ধাপে ধাপে প্রক্রিয়াটি দেখানো হলো:


১. Custom Exception তৈরি করা

কাস্টম এক্সেপশন তৈরি করতে একটি ক্লাস তৈরি করুন যা RuntimeException বা Exception এক্সটেন্ড করে।

উদাহরণ:

public class CustomClientException extends RuntimeException {
    private int statusCode;

    public CustomClientException(String message, int statusCode) {
        super(message);
        this.statusCode = statusCode;
    }

    public int getStatusCode() {
        return statusCode;
    }
}

২. RestTemplate এর জন্য Exception Handling যুক্ত করা

RestTemplate এর সাথে ResponseErrorHandler ব্যবহার করে হ্যান্ডলিং সেটআপ করা যায়।

উদাহরণ:

import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResponseErrorHandler;

import java.io.IOException;

@Component
public class CustomResponseErrorHandler implements ResponseErrorHandler {

    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
        return response.getStatusCode().isError();
    }

    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        // Convert error response to custom exception
        throw new CustomClientException("Error occurred: " + response.getStatusText(), response.getStatusCode().value());
    }
}

৩. RestTemplate এর সাথে Custom Error Handler কনফিগার করা

Custom Error Handler যুক্ত করতে RestTemplate বীন কনফিগার করুন।

উদাহরণ:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    private final CustomResponseErrorHandler errorHandler;

    public AppConfig(CustomResponseErrorHandler errorHandler) {
        this.errorHandler = errorHandler;
    }

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setErrorHandler(errorHandler);
        return restTemplate;
    }
}

৪. Exception Handling যুক্ত করে API কল করা

API কল করার সময় কাস্টম এক্সেপশন হ্যান্ডল করতে try-catch ব্লক ব্যবহার করুন।

উদাহরণ:

import org.springframework.stereotype.Service;

@Service
public class ApiService {
    private final RestTemplate restTemplate;

    public ApiService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String fetchData(String url) {
        try {
            return restTemplate.getForObject(url, String.class);
        } catch (CustomClientException ex) {
            System.err.println("Custom Exception: " + ex.getMessage() + ", Status Code: " + ex.getStatusCode());
            return "Error occurred while fetching data.";
        }
    }
}

৫. Controller Advice দিয়ে Global Exception Handling

স্প্রিং বুটে গ্লোবাল এক্সেপশন হ্যান্ডলিং করতে @ControllerAdvice ব্যবহার করতে পারেন।

উদাহরণ:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomClientException.class)
    public ResponseEntity<String> handleCustomClientException(CustomClientException ex) {
        return ResponseEntity
                .status(ex.getStatusCode())
                .body("Custom Error: " + ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleGenericException(Exception ex) {
        return ResponseEntity
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("An unexpected error occurred: " + ex.getMessage());
    }
}

৬. ব্যবহার এবং ফলাফল

API কল করার সময় যদি সার্ভার কোনো এরর রেসপন্স করে, CustomResponseErrorHandler সেই এরর ক্যাপচার করবে এবং কাস্টম এক্সেপশন থ্রো করবে।

উদাহরণ:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Client {
    @Autowired
    private ApiService apiService;

    public void fetchDataExample() {
        String url = "http://example.com/api/resource";
        String response = apiService.fetchData(url);
        System.out.println("Response: " + response);
    }
}

এরর রেসপন্স:

  • যদি সার্ভার থেকে 404 Not Found আসে:

    Custom Exception: Error occurred: Not Found, Status Code: 404
    
  • গ্লোবাল হ্যান্ডলিং (Controller Advice) থাকলে কাস্টম রেসপন্স রিটার্ন করবে:

    {
      "status": 404,
      "message": "Custom Error: Error occurred: Not Found"
    }
    

সংক্ষেপে:

  1. Custom Exception: RuntimeException থেকে এক্সটেন্ড করুন।
  2. ResponseErrorHandler: RestTemplate এর এরর হ্যান্ডলিং সেটআপ করুন।
  3. Global Exception Handling: @ControllerAdvice দিয়ে গ্লোবাল লেভেলে হ্যান্ডল করুন।
  4. Usage: API কলের সময় try-catch দিয়ে সুনির্দিষ্ট হ্যান্ডলিং করুন।

এইভাবে আপনি স্প্রিং বুট ক্লায়েন্টে কাস্টম এক্সেপশন তৈরি এবং পরিচালনা করতে পারবেন।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...